#ifndef _USER_APP_H_
#define _USER_APP_H_

#include "Global.h"

#define IR_CODE_MODE1_KEY1   0x01
#define IR_CODE_MODE1_KEY2   0x02
#define IR_CODE_MODE1_KEY3   0x03
#define IR_CODE_MODE1_KEY4   0x04
#define IR_CODE_MODE1_KEY5   0x05
#define IR_CODE_MODE1_KEY6   0x06
#define IR_CODE_MODE1_KEY7   0x2F
#define IR_CODE_MODE1_KEY8   0x42
#define IR_CODE_MODE2_KEY1   0x09
#define IR_CODE_MODE2_KEY2  0x0A
#define IR_CODE_MODE2_KEY3  0x0B
#define IR_CODE_MODE2_KEY4  0x0C
#define IR_CODE_MODE2_KEY5  0x0D
#define IR_CODE_MODE2_KEY6  0x0E
#define IR_CODE_MODE2_KEY7  0x11
#define IR_CODE_MODE2_KEY8  0x24

#define IR_CMD_LONGTOU          IR_CODE_MODE1_KEY1
#define IR_CMD_PUTONGDACHONG    IR_CODE_MODE1_KEY2
#define IR_CMD_PUTONGXIAOCHONG  IR_CODE_MODE1_KEY3
#define IR_CMD_LINYUQI          IR_CODE_MODE1_KEY4
#define IR_CMD_JIENENGDACHONG   IR_CODE_MODE1_KEY5
#define IR_CMD_JIENENGXIAOCHONG IR_CODE_MODE1_KEY6
#define IR_CMD_LIANGDUANDACHONG IR_CODE_MODE1_KEY7
#define IR_CMD_GANYINGKAIGUAN   IR_CODE_MODE1_KEY8

#define IR_CMD_YANSEQIEHUAN     0
#define IR_CMD_SHIJIANFUWEI     IR_CODE_MODE2_KEY1
#define IR_CMD_FASHEGONGLV      IR_CODE_MODE2_KEY2
#define IR_CMD_ZIDONGJULI       IR_CODE_MODE2_KEY3
#define IR_CMD_JIXINGQIEHUAN    IR_CODE_MODE2_KEY4
#define IR_CMD_SHIJIANJIA       IR_CODE_MODE2_KEY5
#define IR_CMD_SHIJIANJIAN      IR_CODE_MODE2_KEY6
#define IR_CMD_JULIJIA          IR_CODE_MODE2_KEY7
#define IR_CMD_JULIJIAN         IR_CODE_MODE2_KEY8

#define C_STEP_NORMAL     5
#define C_STEP_LONG       25
#define C_MAX_NORMAL      200
#define C_MAX_LONG        1000

#define C_DEFAULT_SENS_LONGTOU  15
#define C_DEFAULT_SENS_NORMAL   15
#define C_DEFAULT_SENS_LONG     15  //C_MAX_IDX_SENS_LONG/2 //C_MAX_IDX_SENS_LONG - 1

#define EEP_ADDR_DISTANCE_MODE	0x01
#define EEP_ADDR_SENS_IDX       0x02
#define EEP_ADDR_TASK_ACTION    0x03
#define EEP_ADDR_VLAVE_DIR      0x04
#define EEP_ADDR_TM_ADJ         0x05
#define EEP_ADDR_LED_SEL        0x06

#define LOW_POWER_VOLT          45
#define LOW_POWER_RESUME_VOLT   55

#define C_MIN_BAT_AD            (LOW_POWER_VOLT*62/10)          //((LOW_POWER_VOLT*1024/5)/33)    //248      //(4V/5/3.3V)*1024
#define C_MIN_BAT_RESUME        (LOW_POWER_RESUME_VOLT*62/10)   //((LOW_POWER_RESUME_VOLT*1024/5)/33)    //310    //(5V/5/3.3V)*1024

#define C_TM_INFINITE           255     

enum
{
    TASK_LONG_TOU = 0,
    TASK_DA_CHONG,
    TASK_XIAO_CHONG,
    TASK_LIN_YU_QI,
    TASK_JIE_NENG_DA_CHONG,
    TASK_JIE_NENG_XIAO_COHNG,
    TASK_LIANG_DUAN_DA_CHONG,
    TASK_GAN_YING_KAI_GUAN,
};

#define C_DEFAULT_TASK_ACTION   TASK_LONG_TOU


enum
{
    ACT_NULL=0,             //---- 不动作
    ACT_OFF,                //---- 关闭磁阀
    ACT_ON,                 //---- 开启磁阀
    ACT_INVERT,           //---- 翻转磁阀
};


#define _OLD_ACTION_    (0)

#if _OLD_ACTION_
typedef struct 
{
    uint8   TmDbcOn;
    uint8   ActionOn;
    uint8   TmDlyOn;
    uint8   ActionDlyOn;

    uint8   TmDbcOff;
    uint8   ActionOff;
    uint8   TmDlyOff;
    uint8   ActionDlyOff;

}Typedef_ACTION;
#else
typedef struct 
{
    uint8   TmDbcOn;      //---- 感应消抖时间 ----//
    uint8   ActOn;        //---- 感应动作 ----//
    uint8   TmChkOff;       //---- 判断释放时间 ----//
    uint8   TmActOn;      //---- 感应动作时间 ----//
    uint8   TmAdjust;     //---- 是否需要加入调整时间 ----//

    uint8   TmDbcOff;     //---- 释放消抖时间 ----//
    uint8   TmChkOn;        //---- 判断感应时间 ----//
    uint8   ActOff;       //---- 释放动作 ----// 
    int8    TmActOff1;      //---- 释放动作时间 ----//
    int8    TmActOff2;      //---- 释放动作时间2 ----//
}Typedef_ACTION;
#endif


typedef struct 
{
    uint8   TaskMode;
    uint8   DistMode;
    uint8   SendIdx;
}Typedef_MODE_INIT;

#define C_MAX_TM_ADJ        10
#define C_MIN_TM_ADJ        -10

#define C_MAX_TM_IR_ON      120
#define C_MAX_TM_IR_RELEASE 120

//---- 龙头配置 ----//
#define LT_TM_DBC_ON        0
#define LT_ACT_ON           ACT_ON
#define LT_TM_CHK_OFF       0
#define LT_ACT_ADJ          0
#define LT_TM_ACT_ON        60
#define LT_TM_DBC_OFF       0
#define LT_TM_CHK_ON        0
#define LT_ACT_OFF          ACT_OFF
#define LT_TM_ACT_OFF1      0
#define LT_TM_ACT_OFF2      0

//---- 普通大冲配置 ----//
#define PTDC_TM_DBC_ON      3
#define PTDC_ACT_ON         ACT_NULL
#define PTDC_TM_CHK_OFF     0
#define PTDC_ACT_ADJ        1
#define PTDC_TM_ACT_ON      3
#define PTDC_TM_DBC_OFF     2
#define PTDC_TM_CHK_ON      0
#define PTDC_ACT_OFF        ACT_ON
#define PTDC_TM_ACT_OFF1    12
#define PTDC_TM_ACT_OFF2    12

//---- 普通小冲配置 ----//
#define PTXC_TM_DBC_ON      3
#define PTXC_ACT_ON         ACT_ON
#define PTXC_TM_CHK_OFF     0
#define PTXC_ACT_ADJ        1
#define PTXC_TM_ACT_ON      3
#define PTXC_TM_DBC_OFF     0
#define PTXC_TM_CHK_ON      0
#define PTXC_ACT_OFF        ACT_ON
#define PTXC_TM_ACT_OFF1    6
#define PTXC_TM_ACT_OFF2    6

//---- 淋浴器配置 ----//
#define LYQ_TM_DBC_ON       0
#define LYQ_ACT_ON          ACT_ON
#define LYQ_TM_CHK_OFF      0
#define LYQ_ACT_ADJ         0
#define LYQ_TM_ACT_ON       C_TM_INFINITE
#define LYQ_TM_DBC_OFF      3
#define LYQ_TM_CHK_ON       0
#define LYQ_ACT_OFF         ACT_OFF
#define LYQ_TM_ACT_OFF1     0
#define LYQ_TM_ACT_OFF2     0

//---- 节能大冲配置 ----//
#define JNDC_TM_DBC_ON       3
#define JNDC_ACT_ON          ACT_NULL
#define JNDC_TM_CHK_OFF      0
#define JNDC_ACT_ADJ         1
#define JNDC_TM_ACT_ON       0
#define JNDC_TM_DBC_OFF      3
#define JNDC_TM_CHK_ON       60
#define JNDC_ACT_OFF         ACT_ON
#define JNDC_TM_ACT_OFF1     3
#define JNDC_TM_ACT_OFF2     12

//---- 节能大冲配置 ----//
#define JNDC_TM_DBC_ON       3
#define JNDC_ACT_ON          ACT_NULL
#define JNDC_TM_CHK_OFF      0
#define JNDC_ACT_ADJ         1
#define JNDC_TM_ACT_ON       0
#define JNDC_TM_DBC_OFF      3
#define JNDC_TM_CHK_ON       60
#define JNDC_ACT_OFF         ACT_ON
#define JNDC_TM_ACT_OFF1     3
#define JNDC_TM_ACT_OFF2     12

//---- 节能小冲配置 ----//
#define JNXC_TM_DBC_ON       3
#define JNXC_ACT_ON          ACT_ON
#define JNXC_TM_CHK_OFF      60
#define JNXC_ACT_ADJ         1
#define JNXC_TM_ACT_ON       3
#define JNXC_TM_DBC_OFF      0
#define JNXC_TM_CHK_ON       0
#define JNXC_ACT_OFF         ACT_ON
#define JNXC_TM_ACT_OFF1     6
#define JNXC_TM_ACT_OFF2     6

//---- 两段大冲配置 ----//
#define LDDC_TM_DBC_ON       3
#define LDDC_ACT_ON          ACT_ON
#define LDDC_TM_CHK_OFF      0
#define LDDC_ACT_ADJ         1
#define LDDC_TM_ACT_ON       1
#define LDDC_TM_DBC_OFF      2
#define LDDC_TM_CHK_ON       0
#define LDDC_ACT_OFF         ACT_ON
#define LDDC_TM_ACT_OFF1     12
#define LDDC_TM_ACT_OFF2     12

//---- 感应开关配置 ----//
#define GYKG_TM_DBC_ON       0
#define GYKG_ACT_ON          ACT_INVERT
#define GYKG_TM_CHK_OFF      0
#define GYKG_ACT_ADJ         0
#define GYKG_TM_ACT_ON       120
#define GYKG_TM_DBC_OFF      0
#define GYKG_TM_CHK_ON       0
#define GYKG_ACT_OFF         ACT_NULL
#define GYKG_TM_ACT_OFF1     C_TM_INFINITE
#define GYKG_TM_ACT_OFF2     C_TM_INFINITE

#define GYKG_MAX_TIME_ON    120


extern bit F_IR_STATE ;
extern bit F_LowPower;

void    initUserApp(void);
void    DoCommand(void);

void    Action(uint8 action);
void    Action_Proc(void);
void EEPROM_Write(void);
void EEPROM_Read(void);
void LowPowerCheck(void);
#endif